package stack.era.database;

import java.util.ArrayList;

import stack.era.domain.character.Character;
import stack.era.domain.location.Location;
import stack.era.domain.town.Town;
import stack.era.domain.world.World;

public class DatabaseHome {

	private static DatabaseHome instance = null;

	private CharacterSaver characterSaver;
	private TownSaver townSaver;
	private LocationSaver locationSaver;
	private WorldSaver worldSaver;

	private DatabaseHome() {
		characterSaver = new CharacterSaver();
		townSaver = new TownSaver();
		locationSaver = new LocationSaver();
		worldSaver = new WorldSaver();
	}

	public static DatabaseHome getInstance() {
		if (instance == null) {
			instance = new DatabaseHome();
		}
		return instance;
	}

	public void saveCharacters(ArrayList<Character> characters) {
		characterSaver.saveObjects(characters);
	}

	public void saveTowns(ArrayList<Town> towns) {
		townSaver.saveObjects(towns);
	}

	public void saveLocations(ArrayList<Location> locations) {
		locationSaver.saveObjects(locations);
	}

	public void saveWorld(World world) {
		ArrayList<World> list = new ArrayList<World>();
		list.add(world);
		worldSaver.saveObjects(list);
	}

	public ArrayList<Character> loadCharacters() {
		return characterSaver.loadObjects();
	}

	public ArrayList<Town> loadTowns() {
		return townSaver.loadObjects();
	}

	public ArrayList<Location> loadLocations() {
		return locationSaver.loadObjects();
	}

	public World loadWorld() {
		ArrayList<World> worlds = worldSaver.loadObjects();
		if (worlds != null) {
			return worlds.get(0);
		} else {
			return null;
		}
	}

}
